/*
 * This file is part of the Marmalade SDK Code Samples.
 *
 * (C) 2001-2012 Marmalade. All Rights Reserved.
 *
 * This source code is intended only as a supplement to the Marmalade SDK.
 *
 * THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
 * KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
 * PARTICULAR PURPOSE.
 */

// Marmalade includes
#include "IwUIElementEventHandler.h"
#include "IwUITableView.h"

// Project includes
#include "IwUIDailyReport.h"

//-----------------------------------------------------------------------------

class CDailyReportsViewHandler : public CIwUIElementEventHandler
{
public:
    IW_MANAGED_DECLARE(CDailyReportsViewHandler)
        CDailyReportsViewHandler() : m_TableView(NULL) {}

private:
    // CIwUIElementEventHandler virtuals
    virtual void Activate(bool val)
    {
        CIwUIElementEventHandler::Activate(val);

        if (val)
        {
            IW_UI_CREATE_HANDLER_SLOT1(this, CDailyReportsViewHandler,
                CreateDailyReport, CIwUIElement*);
            IW_UI_CREATE_HANDLER_SLOT1(this, CDailyReportsViewHandler,
                DeleteDailyReport, CIwUIElement*);
            IW_UI_CREATE_HANDLER_SLOT1(this, CDailyReportsViewHandler,
                UpdateDailyReport, CIwUIElement*);
            IW_UI_CREATE_HANDLER_SLOT1(this, CDailyReportsViewHandler,
                UpdateAllDailyReports, CIwUIElement*);


	// ---------------------------------- VNThanh ----------------------------------
            IW_UI_CREATE_HANDLER_SLOT1(this, CDailyReportsViewHandler,
                CreateTask, CIwUIElement*);
            IW_UI_CREATE_HANDLER_SLOT1(this, CDailyReportsViewHandler,
                DoneTask, CIwUIElement*);
	// ---------------------------------- End VNThanh ----------------------------------



            GetElement()->LookupChildNamed(m_TableView, "TableView");
        }
        else
        {
            IW_UI_DESTROY_HANDLER_SLOTS(this);

            m_TableView = NULL;
        }
    }

    virtual void Clone(CIwUIElementEventHandler* pTarget) const
    {
        IW_UI_CLONE_SUPERCLASS(pTarget, CDailyReportsViewHandler, CIwUIElementEventHandler)
    }

    // Slot functions
    void CreateDailyReport(CIwUIElement* pCommandElement)
    {
        //GetApp()->ShowCreateDialog();
		GetApp()->CreateTask(pCommandElement);
    }

    void DeleteDailyReport(CIwUIElement* pButtonElement)
    {
        int32 uid = -1;
        pButtonElement->GetParent()->GetProperty("dailyReportUID", uid);
		GetApp()->ShowDeleteDialog(uid);
    }

    void UpdateDailyReport(CIwUIElement* pButtonElement)
    {
        // Should find UID stored of parent of button (the table view item).
        int32 uid = -1;
        pButtonElement->GetParent()->GetProperty("dailyReportUID", uid);

        GetApp()->ShowCreateDialog(uid);
    }

    void UpdateAllDailyReports(CIwUIElement*)
    {
        GetApp()->UpdateAllDailyReports();
    }


	// ---------------------------------- VNThanh ----------------------------------
	// Create Task
	void CreateTask(CIwUIElement* pCommandElement)
	{
		GetApp()->CreateTask(pCommandElement);
	}
	// Done Task
	void DoneTask(CIwUIElement* pCommandElement)
	{
		GetApp()->DoneTask(pCommandElement);
	}
	// ---------------------------------- End VNThanh ----------------------------------


    // Member data
    CIwUITableView* m_TableView;
};

IW_MANAGED_IMPLEMENT_FACTORY(CDailyReportsViewHandler)
